package com.wz.customerserv.feign.fallback;

import com.wz.customerserv.feign.ProducerService;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProducerServiceFallbackFactory  implements FallbackFactory<ProducerService> {

    @Override
    public ProducerService create(Throwable cause) {
        return new ProducerService() {
            @Override
            public String addPrefix(String name) {
                log.info("由于请求异常，将执行服务降级");
                log.info(cause.getMessage());
                if (cause instanceof FeignException) {
                    FeignException feignException = (FeignException) cause;
                    if (feignException.status() == 500) {
                        return "Service error occurred. Please try again later.";
                    } else if (feignException.status() == 503) {
                        return "Service is temporarily unavailable.";
                    }
                }
                return "请求异常";
            }
        };
    }
}
